package com.ddxz.middleground.meta.controller;

import com.ddxz.middleground.meta.dto.MetaHeader;
import com.ddxz.middleground.meta.dto.source.SourceInfo;
import com.ddxz.middleground.meta.dto.source.SourcePage;
import com.ddxz.middleground.meta.service.SourceService;
import com.ddxz.tool.web.annotation.DdxzRequestHeader;
import com.ddxz.tool.web.annotation.DdxzRequestParam;
import com.ddxz.tool.web.dto.DdxzResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * 本类介绍了用户对数据源的基本操作。主要包含以下操作：数据源检索、数据源详情查看、数据源保存、数据源测试、数据源删除、修改数据源状态
 * 如果数据源状态为禁用或者异常，则该数据源下所有的对象都会变为异常状态
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/8/21
 */

@RestController
@Api("数据源管理")
public class SourceController {
    @ApiOperation(value = "数据源检索", produces = "application/json", consumes = "application/json",
            notes = "根据检索参数以分页的形式检索数据源")
    @RequestMapping(value = "/source/list", method = RequestMethod.GET)
    public DdxzResponse sourceList(@DdxzRequestHeader MetaHeader header, @DdxzRequestParam SourcePage page) {
        return service.sourceList(header, page);
    }

    @ApiOperation(value = "查看数据源", produces = "application/json", consumes = "application/json",
            notes = "查看数据源的详细信息")
    @RequestMapping(value = "/source/{sourceId}/detail", method = RequestMethod.GET)
    public DdxzResponse sourceDetail(@DdxzRequestHeader MetaHeader header, @PathVariable("sourceId") String sourceId) {
        return service.sourceDetail(header, sourceId);
    }

    @ApiOperation(value = "保存数据源", produces = "application/json", consumes = "application/json",
            notes = "将用户设定的数据源信息保存到数据库中")
    @RequestMapping(value = "/source/save", method = RequestMethod.POST)
    public DdxzResponse sourceSave(@DdxzRequestHeader MetaHeader header, @RequestBody SourceInfo sourceInfo) {
        return service.sourceSave(header, sourceInfo);
    }

    @ApiOperation(value = "移除数据源", produces = "application/json", consumes = "application/json",
            notes = "删除指定的数据源信息")
    @RequestMapping(value = "/source/{sourceId}/remove", method = RequestMethod.DELETE)
    public DdxzResponse sourceRemove(@DdxzRequestHeader MetaHeader header, @PathVariable("sourceId") String sourceId) {
        return service.sourceRemove(header, sourceId);
    }

    @ApiOperation(value = "测试数据源", produces = "application/json", consumes = "application/json",
            notes = "测试指定的数据源是否正常")
    @RequestMapping(value = "/source/{sourceId}/test", method = RequestMethod.GET)
    public DdxzResponse sourceTest(@DdxzRequestHeader MetaHeader header, @PathVariable("sourceId") String sourceId) {
        return service.sourceTest(header, sourceId);
    }

    @ApiOperation(value = "修改状态", produces = "application/json", consumes = "application/json",
            notes = "修改数据源的运行状态")
    @RequestMapping(value = "/source/{sourceId}/state", method = RequestMethod.PUT)
    public DdxzResponse sourceSetState(@DdxzRequestHeader MetaHeader header, @PathVariable("sourceId") String sourceId, @RequestParam("state") Integer state) {
        return service.setState(header, sourceId, state);
    }

    private final SourceService service;

    public SourceController(SourceService service) {
        this.service = service;
    }
}
